﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using BusinessLayer;
using ModelLayer;
using ModelLayer.Common;
using ShopKart.Areas.User.Models;
using ShopKart.Common;
using ShopKart.Controllers;

namespace ShopKart.Areas.User.Controllers
{
    [AuthUser(Roles = "User")]
    public class UserController : BaseController
    {
        // GET: User/Profile
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public PartialViewResult EditUserProfile()
        {
            UserManager userManager = new UserManager();
            var user = userManager.GetById(SessionObject.UserId);
            UserVm userVm = Mapper.Map<UserVm>(user);
            if (user.UserAddresses != null)
            {
                if (user.UserAddresses.Count > 0)
                {
                    var address = user.UserAddresses.FirstOrDefault();
                    if (address != null)
                    {
                        userVm.PrimaryAddress = address.PrimaryAddress;
                        userVm.SecondaryAddress = address.SecondaryAddress;
                        userVm.City = address.City;
                        userVm.State = address.State;
                        userVm.Country = address.Country;
                        userVm.ZipCode = address.ZipCode;
                    }
                }
            }

            return PartialView("_EditUserProfile", userVm);
        }

        [HttpPost]
        public PartialViewResult EditUserProfile(UserVm userVm)
        {
            UserManager UserManager = new UserManager();

            var allErrors = ModelState.Values.SelectMany(v => v.Errors);
            if (!ModelState.IsValid)
            {
                return PartialView("_EditUserProfile", userVm);
            }

            var user = Mapper.Map<ModelLayer.User>(userVm);
            user.UserAddresses = new List<UserAddress>()
            {
                new UserAddress()
                {
                    PrimaryAddress = userVm.PrimaryAddress,
                    SecondaryAddress = userVm.SecondaryAddress,
                    City= userVm.City,
                    State= userVm.State,
                    Country= userVm.Country,
                    ZipCode= userVm.ZipCode         
                }
            };
            var result = UserManager.UpdateProfile(user);

            if (result) //Success
            {
                SessionObject sessionObject = (SessionObject)Session["SessionObject"];
                sessionObject.User = user;
                Session["SessionObject"] = sessionObject;
                ShowMessage("User Updated Successfully", MessageTypeEnum.Success);
            }
            else //Error
            {
                ShowMessage("Some error occured", MessageTypeEnum.Error);

            }

            return PartialView("_EditUserProfile", userVm);
        }
    }
}